package com.controller.sales;


import com.pojo.requestjson.AddSalesProductDetailRequestJson;
import com.pojo.sales.view.SalesDetailR;
import com.pojo.sales.view.SalesProductR;
import com.pojo.sales.view.SalesTop5ProductView;
import com.service.iter.sales.IProductService;
import com.service.iter.sales.ISaleOrderService;
import com.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 销售控制器
 *
 * @author admin
 * 2023/06/19
 */
@RestController
@RequestMapping("sales/")
public class SaleController {
    /**
     * 产品服务
     */
    @Autowired
    private IProductService productService;
    @Autowired
    private ISaleOrderService salesDetailRService;


    /**
     * 销售产品
     *
     * @return {@link Res}
     */
    @GetMapping("getSaleProducts")
    public Res getSaleProducts() {
        Res res = new Res();
        List<SalesProductR> saleProducts = productService.getSaleProducts();
        if (Objects.isNull(saleProducts)) {
            res.setCode(404);
            res.setMsg("无销售产品");
        } else {
            res.setCode(200);
            res.setData(saleProducts);
        }
        return res;
    }

    /**
     * 前五名销售产品
     *
     * @return {@link Res}
     */
    @GetMapping("getTopFiveSaleProduct")
    public Res getTopFiveSaleProduct() {
        Res res = new Res();
        List<SalesTop5ProductView> topFiveSaleProduct = productService.getTopFiveSaleProduct();
        if (Objects.isNull(topFiveSaleProduct)) {
            res.setCode(404);
            res.setMsg("本月未销售任何产品");
        } else {
            res.setCode(200);
            res.setData(topFiveSaleProduct);
        }
        return res;
    }

    /**
     * 得到销售订单详情
     *
     * @return {@link Res}
     */
    @GetMapping("getSalesDetails")
    public Res getSalesDetails() {
        Res res = new Res();
        List<SalesDetailR> salesDetails = salesDetailRService.getSalesDetail();
        if (Objects.isNull(salesDetails)) {
            res.setCode(404);
            res.setMsg("无销售订单");
        } else {
            res.setCode(200);
            res.setData(salesDetails);
        }
        return res;
    }

    /**
     * 添加销售订单
     *
     * @param products 产品
     * @return {@link Res}
     *///添加销售订单表
    @PostMapping("AddSaleOrder")
    public Res AddSaleOrder(@RequestBody AddSalesProductDetailRequestJson products) {
        return salesDetailRService.AddSaleOrder(products);

    }

    /**
     * 添加产品订单
     *
     * @param salesOrderId 销售订单id
     * @return {@link Res}
     *///添加产品订单表
    @PostMapping("/{salesOrderId}")
    public Res AddProductOrder(@PathVariable Long salesOrderId) {
        return salesDetailRService.AddProductOrder(salesOrderId);
    }

    @GetMapping("/queryOrder")
    public Res queryOrder(Long salesOrderId) {
        return salesDetailRService.updateStatus(salesOrderId);
    }
}
